﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy_dll_inject
{
    /* 数据区、功能函数区 */
    public partial class Form1
    {
        
        private Process[] m_arrayCurProcess;     // 当前所有进程
        private List<string> m_listCurDllFile = new List<string>(); // 当前所有待注入 dll 文件
        private Process selectedProcess = null; // 选择的待注入进程

        // 获取当前所有可用进程，填入 m_listCurProcess;
        private bool GetAllActiveProcess()
        {
            bool ret = false;
            try
            {
                m_arrayCurProcess = Process.GetProcesses();
                Array.Sort<Process>(m_arrayCurProcess, new Comparison<Process>(
                    (a, b) => { return a.ProcessName.CompareTo(b.ProcessName); }));
                ret = true;
            }
            catch (Exception ex)
            {
                Log(ex.ToString(), log);
            }
            return ret;
        }

        // 添加 dll 文件
        private bool AddDllFile()
        {
            bool ret = false;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "F:\\Work\\HOOK\\example 01\\GH_D3D11_Hook";
            openFileDialog1.Filter = "dll files (*dll)|*.dll";
            openFileDialog1.Title = "选择 dll 文件";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string selectedFileName = openFileDialog1.FileName;
                if (!m_listCurDllFile.Contains(selectedFileName))
                {
                    m_listCurDllFile.Add(selectedFileName);
                    ret = true;
                }
                else
                {
                    MessageBox.Show("已经存在该 dll 文件!");
                }
            }
            else
            {
                Log("添加 Dll 文件失败", log);
            }
            return ret;
        }


        // 在 ListView 上显示 所有的 dll 文件
        private void ShowAllDllFileOnListView(ref ListView view)
        {
            view.Items.Clear(); // 清除
            foreach(var file in m_listCurDllFile)
            {
                view.Items.Add(new ListViewItem(file));
            }
        }

        // 删除选择的条目
        private void DeleteSelectListViewItem(ref ListView view)
        {
            try
            {
                for(int i = 0; i < view.SelectedItems.Count; ++i)
                {
                    var item = view.SelectedItems[i];
                    if (m_listCurDllFile.Contains(item.Text))
                    {
                        m_listCurDllFile.Remove(item.Text);
                    }
                }

                ShowAllDllFileOnListView(ref view);

            }catch(Exception ex)
            {
                Log(ex.ToString(), log);
            }
        }

        // 显示所有的进程信息
        private void ShowAllActiveProcess(ref ComboBox cb)
        {
            cb.Items.Clear();

            foreach(var process in m_arrayCurProcess)
            {
                cb.Items.Add(new ComboboxItem("ID:" + process.Id.ToString().PadLeft(5,' ') + "  Name:" + process.ProcessName + ".exe", process));
            }
        }
        public class ComboboxItem
        {
            public string Text { get; set; }
            public Process Value { get; set; }

            public ComboboxItem()
            {
                Text = "NULL";
                Value = null;
            }
            public ComboboxItem(string text, Process value)
            {
                Text = text;
                Value = value;
            }
            public override string ToString()
            {
                return Text;
            }
        }

        public static void Log(string logMessage, TextWriter w)
        {
            w.Write("\r\n日志记录 : ");
            w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
            w.WriteLine("  :");
            w.WriteLine($"  :{logMessage}");
            w.WriteLine("-------------------------------");
        }
        public static StreamWriter log = File.AppendText("log.txt");        // log 文件
    }
}
